package com.tiyende.credit.loop.utils.helper

import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.configs.TiyendeApiPath
import com.tiyende.credit.loop.configs.TiyendeApiUrl
import com.tiyende.credit.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.credit.loop.event.TiyendeEventData
import com.tiyende.credit.loop.request.HttpHelper
import com.tiyende.credit.loop.request.IHttpCallback
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/20 18:06
 * @Annotation：保存自定义数据接口 参数
 */
object TiyendeCustomDataHelper {

    /**
     * userName                         : 用户名
     * userAvatarUrl                    ： 用户头像链接
     */

    /**
     * 获取保存的自定义数据
     */
    fun queryTiyendeCustomData(): JSONObject {
        val customData = TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_CUSTOM_DATA)
        if (customData.isEmpty()){
            return JSONObject()
        }
        return JSONObject(customData)
    }

    /**
     * 获取用户昵称
     */
    fun queryTiyendeCustomUserName(): String{
        val data = queryTiyendeCustomData()
        val defaultUserName = "+${TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_TIMEZONE_CODE)} ${TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_MOBILE)}"
        return data.optString("userName",defaultUserName)
    }

    /**
     * 获取用户头像地址
     */
    fun queryTiyendeCustomUserAvatarUrl(): String{
        val data = queryTiyendeCustomData()
        return data.optString("userAvatarUrl","")
    }

    /**
     * 保存用户昵称
     */
    fun saveTiyendeCustomUserNameAction(userName: String){
        val obj = queryTiyendeCustomData()
        obj.put("userName",userName)
        saveTiyendeCustomDataAction(obj)
    }

    /**
     * 保存用户头像id
     */
    fun saveTiyendeCustomUserAvatar(imgUrl: String){
        val obj = queryTiyendeCustomData()
        obj.put("userAvatarUrl",imgUrl)
        saveTiyendeCustomDataAction(obj)
    }

    /**
     * 获取自定义数据
     */
    fun queryTiyendeCustomDataAction(){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_CUSTOM_PARAMS_PATH)
        }
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null && obj.has("userInfo")){
                            val userInfo = obj.optString("userInfo")
                            //保存自定义数据
                            TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_CUSTOM_DATA,userInfo.toString())
                        }
                        //通知自定义数据刷新完成
                        TiyendeApp.app.publisher.publish(TiyendeEventData(TiyendeEventData.TYPE.TIYENDE_CUSTOM_DATA_REFRESHED))
                    }
                    else -> {
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 保存数据
     */
    private fun saveTiyendeCustomDataAction(obj: JSONObject, callback: IHttpCallback? = null){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_SAVE_CUSTOM_PARAMS_PATH)
            put("userInfo",obj.toString())
        }
        HttpHelper.post(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                callback?.onRequestSuccess(data)
                when(data.optInt("code")) {
                    0 -> {//成功
                        val jsonObj = data.optJSONObject("obj")
                        if (jsonObj != null){
                            //保存成功 再更新数据
                            queryTiyendeCustomDataAction()
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback?.onRequestError(error)
            }

            override fun onRequestFinish() {
                callback?.onRequestFinish()
            }
        })
    }
}